package com.example.yls.demoa;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private  String weatherJson = "{\"date\":\"20180426\",\"message\":\"Success !\",\"status\":200,\"city\":\"广州\",\"count\":931,\"data\":{\"shidu\":\"93%\",\"pm25\":50.0,\"pm10\":72.0,\"quality\":\"良\",\"wendu\":\"20\",\"ganmao\":\"极少数敏感人群应减少户外活动\",\"yesterday\":{\"date\":\"25日星期三\",\"sunrise\":\"06:00\",\"high\":\"高温 26.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:52\",\"aqi\":39.0,\"fx\":\"东北风\",\"fl\":\"3-4级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},\"forecast\":[{\"date\":\"26日星期四\",\"sunrise\":\"05:59\",\"high\":\"高温 27.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:52\",\"aqi\":76.0,\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"阵雨\",\"notice\":\"阵雨来袭，出门记得带伞\"},{\"date\":\"27日星期五\",\"sunrise\":\"05:58\",\"high\":\"高温 26.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:53\",\"aqi\":73.0,\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"中雨\",\"notice\":\"记得随身携带雨伞哦\"},{\"date\":\"28日星期六\",\"sunrise\":\"05:57\",\"high\":\"高温 26.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:53\",\"aqi\":76.0,\"fx\":\"南风\",\"fl\":\"3-4级\",\"type\":\"阵雨\",\"notice\":\"阵雨来袭，出门记得带伞\"},{\"date\":\"29日星期日\",\"sunrise\":\"05:57\",\"high\":\"高温 27.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:53\",\"aqi\":64.0,\"fx\":\"东南风\",\"fl\":\"3-4级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"30日星期一\",\"sunrise\":\"05:56\",\"high\":\"高温 28.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:54\",\"aqi\":55.0,\"fx\":\"东南风\",\"fl\":\"3-4级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"}]}}";
    private WeatherData weatherData;
    private TextView tvNowInfo;
    private TextView tvNowInfo2;
    private ImageView ivWeather_a;
    private ImageView ivWeather_b;
    private ImageView ivWeather_c;
    private ImageView ivWeather_d;
    private ImageView ivWeather_e;
    private Button guangzhou;
    private Button shanghai;
    private Button showCity;
    private Button yangjiang;
    private Button yangchun;
    private TextView data_1;
    private TextView data_2;
    private TextView data_3;
    private TextView data_4;
    private TextView data_5;
    private EditText eCity;
    private TempratureView tempratureView;
    private RequestQueue queue;
    private StringRequest stringRequest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        guangzhou = findViewById(R.id.guangzhou);
        shanghai = findViewById(R.id.shanghai);
        showCity = findViewById(R.id.showCity);
        yangjiang = findViewById(R.id.yangjiang);
        yangchun = findViewById(R.id.yangchun);
        queue = Volley.newRequestQueue(this);
        tvNowInfo = findViewById(R.id.tv_now_weather);
        tvNowInfo2 = findViewById(R.id.tv_now_weather2);
        eCity = findViewById(R.id.editCity);
        weatherData = new Gson().fromJson(weatherJson, WeatherData.class);
        initCity("%E5%8C%97%E4%BA%AC");
        initWeatherImg();
        initTempratureView();
        guangzhou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initCity("%E5%B9%BF%E5%B7%9E");
            }
        });
        shanghai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initCity("%E4%B8%8A%E6%B5%B7");
            }
        });
        showCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //initCity("%E5%8C%97%E4%BA%AC");
                try {
                    initCity(  URLEncoder.encode(eCity.getText().toString(), "UTF-8").replaceAll("\\+","%20"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
        yangjiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initCity("%E9%98%B3%E6%B1%9F");
            }
        });
        yangchun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initCity("%E9%98%B3%E6%98%A5");
            }
        });
    }

    private void initCity(String city) {
        String url ="https://www.sojson.com/open/api/weather/json.shtml?city="+city;
         stringRequest = new StringRequest(Request.Method.GET,url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                weatherData = new Gson().fromJson(s, WeatherData.class);
                StringBuffer buffer = new StringBuffer();
                buffer.append("城市: ").append(weatherData.getCity());
                buffer.append("\n湿度: ").append(weatherData.getData().getShidu());
                buffer.append("\nPM2.5: ").append(weatherData.getData().getPm25());
                buffer.append("\nPM10: ").append(weatherData.getData().getPm10());
                buffer.append("\n空气质量: ").append(weatherData.getData().getQuality());
                buffer.append("\n温度: ").append(weatherData.getData().getWendu());
                tvNowInfo.setText(buffer.toString());

                StringBuffer buffer1 = new StringBuffer();
                buffer1.append("周"+weatherData.getData().getForecast().get(0).getDate().substring(5,6)+":").append(weatherData.getData().getForecast().get(0).getNotice());
                buffer1.append("\n"+"周"+weatherData.getData().getForecast().get(1).getDate().substring(5,6)+":").append(weatherData.getData().getForecast().get(1).getNotice());
                buffer1.append("\n"+"周"+weatherData.getData().getForecast().get(2).getDate().substring(5,6)+":").append(weatherData.getData().getForecast().get(2).getNotice());
                buffer1.append("\n"+"周"+weatherData.getData().getForecast().get(3).getDate().substring(5,6)+":").append(weatherData.getData().getForecast().get(3).getNotice());
                buffer1.append("\n"+"周"+weatherData.getData().getForecast().get(4).getDate().substring(5,6)+":").append(weatherData.getData().getForecast().get(4).getNotice());
                tvNowInfo2.setText(buffer1.toString());

                initWeatherImg();
                initTempratureView();
               Log.e("luyunshi",s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("luyunshi","");
            }
        });
        queue.add(stringRequest);

    }
    private void initTempratureView() {
        tempratureView = findViewById(R.id.tempView);
        List<WeatherData.DataBean.ForecastBean> forecast = weatherData.getData().getForecast();
        tempratureView.setData(forecast);
    }

    private void initWeatherImg() {
        data_1 = findViewById(R.id.data_1);
        data_2 = findViewById(R.id.data_2);
        data_3 = findViewById(R.id.data_3);
        data_4 = findViewById(R.id.data_4);
        data_5 = findViewById(R.id.data_5);

        ivWeather_a = findViewById(R.id.image_1);
        ivWeather_b = findViewById(R.id.image_2);
        ivWeather_c = findViewById(R.id.image_3);
        ivWeather_d = findViewById(R.id.image_4);
        ivWeather_e = findViewById(R.id.image_5);

        List<WeatherData.DataBean.ForecastBean> forecast = weatherData.getData().getForecast();

        ivWeather_a.setImageResource(getWeatherImage(forecast.get(0).getType()));
        ivWeather_b.setImageResource(getWeatherImage(forecast.get(1).getType()));
        ivWeather_c.setImageResource(getWeatherImage(forecast.get(2).getType()));
        ivWeather_d.setImageResource(getWeatherImage(forecast.get(3).getType()));
        ivWeather_e.setImageResource(getWeatherImage(forecast.get(4).getType()));
        data_1.setText(forecast.get(0).getDate());
        data_2.setText(forecast.get(1).getDate());
        data_3.setText(forecast.get(2).getDate());
        data_4.setText(forecast.get(3).getDate());
        data_5.setText(forecast.get(4).getDate());

    }
    // 晴 , 多云 , 阴 , 阵雨 , 雷阵雨 , 小雨 , 中雨 , 大雨 , 暴雨 , 大暴雨
    private int getWeatherImage(String type) {
        if(type.equals("晴")){
            return R.drawable.b;
        }else if(type.equals("多云")){
            return R.drawable.c;
        }else if(type.equals("阴")){
            return R.drawable.c;
        }else if(type.equals("阵雨")){
            return R.drawable.f;
        }else if(type.equals("雷阵雨")){
            return R.drawable.d;
        }else if(type.equals("小雨")){
            return R.drawable.h;
        }else if(type.equals("中雨")){
            return R.drawable.e;
        }else if(type.equals("大雨")){
            return R.drawable.e;
        }else if(type.equals("暴雨")){
            return R.drawable.g;
        }

        return R.drawable.sunny;
    }













}
